package com.mini.util;

import groovy.util.logging.Slf4j;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA.
 * User: xiyuefei
 * Date: 2020/3/12
 * Time: 下午1:18
 */
@Slf4j
public class Md5Util {

    public static String getMd516Str(String signString){
        byte[] bytes = null;
        // 使用MD5对待签名串求签
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            bytes = md5.digest(signString.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 将MD5输出的二进制结果转换为小写的十六进制
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                sign.append("0");
            }
            sign.append(hex);
        }
        String sigReturn = sign.toString();
        return sigReturn.substring(8,24);
    }

    public static void main(String[] args) {
        System.out.print(getMd516Str("1"));
    }
}


